package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.PostsBean;
import com.project.bean.UserBean;
import com.project.service.IPostsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

@RestController
@RequestMapping("posts")
public class PostsController {
    @Autowired
    private IPostsService service;
    @RequestMapping("selectByItem")
    public IPage<PostsBean> selectByItem(Integer pageNO, String theme, String author,
                                         LocalDate startDate, LocalDate endDate, Integer channelId){
      return   service.selectByItem(pageNO, theme, author, startDate, endDate, channelId);
    }
    @RequestMapping("selectPosts")
    public PostsBean selectPosts(Integer postsId){
        return  service.selectPosts(postsId);
    }

    @RequestMapping("addPosts")
    public String addPosts(PostsBean posts, HttpServletRequest req){
        //先从session中，得到登陆用户
        UserBean user =(UserBean) req.getSession().getAttribute("loginUser");
        posts.setWriterId(user.getId());
        service.addPosts(posts);
        return "ok";
    }
}
